package com.wyz.module.user.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.istack.internal.NotNull;
import com.wyz.module.comm.util.ResponseResult;
import com.wyz.module.config.security.service.MyDbUserDetailsManager;
import com.wyz.module.user.model.dto.UserDTO;
import com.wyz.module.user.model.vo.UserVO;
import com.wyz.module.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

/**
 * @author 无涯子
 * @version 1.0
 * @date 2024/4/13 15:03:11
 */

@RestController
@RequestMapping("/users")
@Api(tags = "用户模块")
public class UserController {
    @Resource
    private UserService userService;

    @GetMapping("{id}")
    @ApiOperation("根据ID查询用户信息")
    public ResponseResult<UserVO> getUserById(@PathVariable @NotNull Integer id){
        return ResponseResult.success(userService.getUserById(id));
    }

    @GetMapping
    @ApiOperation("查询所有用户信息")
    public ResponseResult<List<UserVO>> getUserAll(){
        // 获取用户认证上下文
        SecurityContext context = SecurityContextHolder.getContext();
        // 获取认证信息
        Authentication authentication = context.getAuthentication();
        // 获取身份信息
        Object principal = authentication.getPrincipal();
        // 获取凭证信息
        Object credentials = authentication.getCredentials();
        // 获取权限信息
        Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();

        System.out.println(authentication.getName());
        System.out.println(authorities);
        return ResponseResult.success(userService.getUserAll());
    }

    @PostMapping
    @ApiOperation("新增用户信息")
    public ResponseResult<String> saveUser(@RequestBody @Validated UserDTO userDTO) throws Exception{
        userService.saveUser(userDTO);
        return ResponseResult.success();
    }

    @PutMapping
    @ApiOperation("更新用户信息")
    public ResponseResult<String> updateUser(@RequestBody UserDTO userDTO) throws Exception{
        userService.updateUser(userDTO);
        return ResponseResult.success();
    }

    @DeleteMapping("/{id}")
    @ApiOperation("根据ID删除用户信息")
    public ResponseResult<String> deleteUser(@PathVariable Integer id) throws Exception{
        userService.removeUserById(id);
        return ResponseResult.success();
    }

    @GetMapping("/{current}/{size}")
    @ApiOperation("分页查询")
    public ResponseResult<Page<UserVO>> getPage(@PathVariable(value = "current") Long current, @PathVariable(value = "size") Long size){
        return ResponseResult.success(userService.getPage(current,size));
    }
}
